﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Win32;

namespace SimpleFileExplorerWPF
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            PopulateTreeView();
        }

        private void OpenFolder_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog
            {
                ValidateNames = false,
                CheckFileExists = false,
                CheckPathExists = true,
                FileName = "Folder Selection."
            };
            if (dialog.ShowDialog() == true)
            {
                string folderPath = Path.GetDirectoryName(dialog.FileName);
                var rootNode = new TreeViewItem { Header = new DirectoryInfo(folderPath).Name, Tag = new DirectoryInfo(folderPath) };
                treeView.Items.Add(rootNode);
                PopulateTreeView(rootNode);
            }
        }

        private void PopulateTreeView(TreeViewItem parentNode = null)
        {
            DirectoryInfo info;
            if (parentNode == null)
            {
                info = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
                parentNode = new TreeViewItem { Header = info.Name, Tag = info };
                treeView.Items.Add(parentNode);
            }
            else
            {
                info = (DirectoryInfo)parentNode.Tag;
            }

            foreach (DirectoryInfo dir in info.GetDirectories())
            {
                TreeViewItem node = new TreeViewItem { Header = dir.Name, Tag = dir };
                parentNode.Items.Add(node);
            }
            parentNode.IsExpanded = true;
        }

        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (treeView.SelectedItem is TreeViewItem selectedItem)
            {
                listView.Items.Clear();
                DirectoryInfo directoryInfo = (DirectoryInfo)selectedItem.Tag;
                foreach (DirectoryInfo dir in directoryInfo.GetDirectories())
                {
                    listView.Items.Add(new ListViewItem { Content = dir.Name });
                }
                foreach (FileInfo file in directoryInfo.GetFiles())
                {
                    listView.Items.Add(new ListViewItem { Content = file.Name });
                }
            }
        }

        private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (listView.SelectedItem is ListViewItem item && treeView.SelectedItem is TreeViewItem selectedNode)
            {
                DirectoryInfo directoryInfo = (DirectoryInfo)selectedNode.Tag;
                string fullPath = Path.Combine(directoryInfo.FullName, item.Content.ToString());
                if (File.Exists(fullPath) && fullPath.EndsWith(".txt"))
                {
                    Process.Start("notepad.exe", fullPath);
                }
            }
        }
    }
}
